package cn.virens.web.components.websocket;

import java.security.Principal;
import java.util.Objects;

import cn.virens.common.ObjectUtil;
import cn.virens.database.mysql.model.sys.SysUser;

public class SimplePrincipal implements Principal {
	private String avatar;
	private String name;
	private String uuid;

	public SimplePrincipal() {
	}

	protected SimplePrincipal(SysUser sysUser) {
		this.avatar = sysUser.getAvatar();
		this.uuid = sysUser.getUuid();
		this.name = sysUser.getName();
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(Object uuid) {
		this.uuid = ObjectUtil.toString(uuid);
	}

	@Override
	public String getName() {
		return name;
	}

	public void setName(Object name) {
		this.name = ObjectUtil.toString(name);
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(Object avatar) {
		this.avatar = ObjectUtil.toString(avatar);
	}

	@Override
	public boolean equals(Object obj) {
		return Objects.hashCode(obj) == hashCode();
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(uuid);
	}

	@Override
	public String toString() {
		return "SimplePrincipal [name=" + name + ", uuid=" + uuid + "]";
	}

	public static String getUuid(Principal principal) {
		if (principal instanceof SimplePrincipal) {
			return ((SimplePrincipal) principal).getUuid();
		} else {
			return null;
		}
	}
}
